package com.example.consumer;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;

public class DemoMessageListener implements MessageListener {

    private static final Logger logger = LoggerFactory.getLogger(DemoMessageListener.class);

    @Override
    public void onMessage(Message message) {
        try {
            String body = new String(message.getBody(), "UTF-8");
            logger.info("接收到消息: {}", body);
            logger.info("消息属性: {}", message.getMessageProperties());

            // 处理消息的业务逻辑
            processMessage(body);

        } catch (Exception e) {
            logger.error("处理消息时发生异常", e);
        }
    }

    private void processMessage(String message) {
        // 这里是具体的业务处理逻辑
        logger.info("开始处理消息: {}", message);

        // 模拟业务处理
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }

        logger.info("消息处理完成");
    }
}
